package com.backend.demo.exception;

import java.io.PrintStream;
import java.io.PrintWriter;
import java.util.ResourceBundle;

/**
 * 异常公共类
 * @author Administrator
 *
 */
public class AppException extends RuntimeException {

	private static final long serialVersionUID = -3620827757855210270L;
	private static final ResourceBundle ERROR_MSG_BUNDLE = ResourceBundle.getBundle("exceptions");
	private Throwable rootcause;
	private Object[] args;
	private String errorCode = null;
	private String errorMessage = null;
	
	public String getErrorCode() {
		return errorCode;
	}
	public void setErrorCode(String errorCode) {
		this.errorCode = errorCode;
	}

	public Object[] getArgs() {
		return args;
	}
	public void setArgs(Object[] args) {
		this.args = args;
	}
	public Throwable getRootcause() {
		return rootcause;
	}
	public AppException(String message) {
		super(getErrorMsg(message));
	}
	
	public AppException(String[] info,Throwable cause) {
		super("错误代码:" + info[0] + ",错误描述：" + info[1]);
		this.errorCode = info[0];
		this.setErrorMessage(info[1]);
		rootcause = cause;
	}
	
	public AppException(String[] info) {
		super("错误代码:" + info[0] + ",错误描述：" + info[1]);
		this.errorCode = info[0];
		this.setErrorMessage(info[1]);
	}
	
	public AppException(String errorCode,String errorMsg) {
		super("错误代码:" + errorCode + ",错误描述：" + errorMsg);
		this.errorCode = errorCode;
		this.setErrorMessage(errorMsg);
	}
	
	public AppException(String errorCode,String errorMsg,Throwable cause) {
		super("错误代码:" + errorCode + ",错误描述：" + errorMsg);
		this.errorCode = errorCode;
		this.setErrorMessage(errorMsg);
		rootcause = cause;
	}

	private static String getErrorMsg(String errorCode){
		String errorMsg = null;
		try{
			errorMsg = ERROR_MSG_BUNDLE.getString(errorCode);
		}catch(Throwable e){
			errorMsg = null;
		}
		if(errorMsg!=null){
			return "错误代码:" + errorCode + ",错误描述："+errorMsg;
		}else{
			return errorCode;
		}
	}
	
	private static String getErrorMsgWithParameters(String errorCode,Object...args){
		String errorMsg = getErrorMsg(errorCode);
		if(args!=null && args.length>0){
			for(int i = 0;i<args.length;i++){
				errorMsg=errorMsg.replace("{"+i+"}", String.valueOf(args[i]));
			}
		}
		return errorMsg;
	}
	public AppException(String message,Object... args) {
		super(getErrorMsgWithParameters(message,args));
		this.args = args;
		this.errorCode = message;
	}

	public AppException(Throwable cause) {
		rootcause = cause;
	}

	public AppException(String message, Throwable cause) {
		super(getErrorMsg(message));
		rootcause = cause;
		this.errorCode = message;
	}

	public AppException(String message, Throwable cause,Object... args) {
		super(getErrorMsgWithParameters(message,args));
		rootcause = cause;
		this.args = args;
		this.errorCode = message;
	}

	public String getMessage() {
		if (rootcause == null)
			return super.getMessage();
		else
			return super.getMessage() + "; cause exception is: \n\t"
					+ rootcause.toString();
	}

	public void printStackTrace() {
		printStackTrace(System.err);
	}

	public void printStackTrace(PrintStream s) {
		if (rootcause == null) {
			super.printStackTrace(s);
		} else {
			s.println(this);
			rootcause.printStackTrace(s);
		}
	}

	public void printStackTrace(PrintWriter s) {
		if (rootcause == null) {
			super.printStackTrace(s);
		} else {
			s.println(this);
			rootcause.printStackTrace(s);
		}
	}
	public String getErrorMessage() {
		return errorMessage;
	}
	public void setErrorMessage(String errorMessage) {
		this.errorMessage = errorMessage;
	}

}
